package com.example.firsttest;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.test.util.ExceptionCollector;

import java.time.Duration;

public class AutoTest03 {

    public WebDriver openExplore(){
        System.setProperty("webdriver.edge.driver", "D:/driver/msedgedriver.exe");
        EdgeOptions options = new EdgeOptions();
        // 允许访问所有链接
        options.addArguments("--remote-allow-origins=*");
////         添加无头模式
//        options.addArguments("-headless");
        // 默认值 等待所有资源加载完，才继续执行
//        options.setPageLoadStrategy(PageLoadStrategy.NORMAL);
//        // DOM 访问已准备就绪，但是还存在其他的资源还在加载
//        options.setPageLoadStrategy(PageLoadStrategy.EAGER);
        // 完全不会阻塞 webDriver
        options.setPageLoadStrategy(PageLoadStrategy.NONE);
        EdgeDriver driver = new EdgeDriver(options);
        driver.get("https://www.baidu.com/");
        return driver;
    }

    public void quitExplore(WebDriver driver){
        driver.quit();
    }

    public void test_01() throws InterruptedException {
        WebDriver driver = openExplore();
        // 设置隐式等待
//        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));

//        driver.findElement(By.cssSelector("#kw")).sendKeys("riize");
//        driver.findElement(By.cssSelector("#su")).click();
//        Thread.sleep(3000);

//        driver.findElement(By.xpath("//*[@id=\"1\"]/div/div/div/div/div/div[2]/div/div[1]/div[1]/div/div/p/span[1]/span"));

        // 强制等待
//        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(3));
//        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"1\"]/div/div/div/div/div/div[2]/div/div[1]/div[1]/div/div/p/span[1]/span")));


        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(3));
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#su")));
        driver.findElement(By.cssSelector("#su")).click();

        quitExplore(driver);
    }

    public void test_02() throws InterruptedException {
        WebDriver driver = openExplore();
        Thread.sleep(3000);

        // 回退——（百度首页——标签页）
        driver.navigate().back();
        Thread.sleep(3000);

        // 前进——（标签页——百度首页）
        driver.navigate().forward();
        Thread.sleep(3000);

        // 刷新——（刷新百度首页）
        driver.navigate().refresh();
        Thread.sleep(3000);

        quitExplore(driver);
    }

    public void test_03() throws InterruptedException {
        WebDriver driver = openExplore();

        driver.get("file:///D:/112/selenium-html/selenium-html/Prompt.html");
        Thread.sleep(3000);

        // 点击——唤起弹窗
        driver.findElement(By.xpath("/html/body/input")).click();
        Thread.sleep(3000);

        // 关闭弹窗
        // 切换句柄
        Alert alert = driver.switchTo().alert();

        // 输入文本
        alert.sendKeys("旺旺碎冰冰");
        Thread.sleep(3000);

        // 点击确认
        alert.accept();
        Thread.sleep(3000);

        // 刷新——回到页面初始状态
        driver.navigate().refresh();
        // 调用弹窗
        driver.findElement(By.xpath("/html/body/input")).click();
        Thread.sleep(3000);
        
        // 获取弹窗文本
        String text = alert.getText();
        System.out.println("弹窗文本为：" + text);
        // 点击取消
        alert.dismiss();
        Thread.sleep(3000);

        quitExplore(driver);
    }

    public void test_04() throws InterruptedException {
        WebDriver driver = openExplore();
        driver.get("file:///D:/112/selenium-html/selenium-html/Prompt.html");
        Thread.sleep(3000);

        // 判断点击弹窗入口元素是否存在
        driver.findElement(By.xpath("/html/body/input"));
        Thread.sleep(3000);

        // 调起弹窗
        driver.findElement(By.xpath("/html/body/input")).click();
        Thread.sleep(3000);

        // 取消弹窗
        Alert alert = driver.switchTo().alert();
        alert.dismiss();

        // 判断点击弹窗入口元素是否存在
        driver.findElement(By.xpath("/html/body/input"));
        Thread.sleep(3000);

        quitExplore(driver);

    }

    public void test_05() throws InterruptedException {
        WebDriver driver = openExplore();
        driver.get("file:///D:/112/selenium-html/selenium-html/upload.html");

        Thread.sleep(3000);
        driver.findElement(By.xpath("/html/body/div/div/input")).sendKeys("D:\\a.txt");
        Thread.sleep(3000);

        quitExplore(driver);
    }

    public void test_06(){
        WebDriver driver = openExplore();

        driver.get("https://www.bilibili.com/");
        System.out.println(driver.getCurrentUrl());

        quitExplore(driver);
    }
}
